<template>
	<view class="container-editor">
		<view class="textarea">
			<view class="page-body">
				<view class='wrapper'>
					<PickerColor ref="colorPicker" :color="{r: 255,g: 0,b: 0,a: 0.6}" @confirm="confirm"></PickerColor>
					<view class='toolbar' @tap="format">
						<view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-font-size"
							data-name="fontSize" data-value="24px"></view>
						<view :class="formats.color? 'ql-active' : ''" class="iconfont icon-zitiyanse" data-name="color" :data-value="formats.color">
						</view>
						<view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold">
						</view>
						<view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti"
							data-name="italic"></view>
						<view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian"
							data-name="underline"></view>
						<view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-shanchuxian"
							data-name="strike"></view>
						<view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi"
							data-name="align" data-value="left"></view>
						<view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi"
							data-name="align" data-value="center"></view>
						<view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi"
							data-name="align" data-value="right"></view>
						<view class="iconfont icon-fengexian" @tap="insertDivider"></view>
						<view class="iconfont icon-charutupian" @tap="insertImage"></view>
					</view>
					<editor id="editor" class="editor" placeholder="请输入产品卖点" showImgSize showImgToolbar showImgResize
						@statuschange="onStatusChange" :read-only="readOnly" @ready="onEditorReady"
						@input="saveContens">
					</editor>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import PickerColor from "./color-picker.vue"
	export default {
		components: {
			PickerColor
		},
		props: {
			api: {
				type: String,
				default: ''
			},
			photoUrl: {
				type: String,
				default: ''
			},
			values: {
				type: String,
				default: ''
			},
			readOnly: {
				type: Boolean,
				default: false
			},
			maxlength: {
				type: Number,
				default: 300
			},
			name: {
				type: String,
				default: 'file'
			}
		},
		data() {
			return {
				currentTab: 0,
				curColor: '#000000',
				show: true,
				hdid: "",
				myHtml: "",
				formats: {}
			}
		},
		methods: {
			showPicker() {
				this.$refs.colorPicker.open()
			},
			confirm(e) {
				this.editorCtx.format('color', e.hex)
			},
			saveContens() {
				let that = this;
				let maxlength = parseInt(that.maxlength);
				that.editorCtx.getContents({
					success: function(res) {
						let html_text = res.html;
						let html_length = html_text.length;
						if (html_length > maxlength) {
							uni.showModal({
								title: '最多只能输入' + maxlength + '字',
								confirmText: '确定',
								showCancel: false,
								success(res) {
									that.$emit("changes", {
										html: res.html,
										length: html_length
									});
								}
							});
						} else {
							that.$emit("changes", {
								html: res.html,
								length: html_length
							});
						}
					},
				})
			},
			update() {
				//获取一下是否有数据
				let that = this;
				setTimeout(() => {
					that.editorCtx.setContents({
						"html": that.values
					});
				}, 1000)
			},
			onEditorReady() {
				let that = this;
				console.log(uni.createSelectorQuery().in(this).select('#editor'))
				uni.createSelectorQuery().in(this).select('#editor').context((res) => {
					that.editorCtx = res.context;
					that.update();
				}).exec((result) => {});
			},
			undo() {
				this.editorCtx.undo()
			},
			redo() {
				this.editorCtx.redo()
			},
			format(e) {
				let {
					name,
					value
				} = e.target.dataset
				if (!name) return
				if (name == 'color') {
					this.showPicker()
				} else {
					this.editorCtx.format(name, value)
				}
			},
			onStatusChange(e) {
				const formats = e.detail
				this.formats = formats
			},
			insertDivider() {
				this.editorCtx.insertDivider();
			},
			clear() {
				this.editorCtx.clear();
				this.$emit()
			},
			insertDate() {
				const date = new Date()
				const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
				this.editorCtx.insertText({
					text: formatDate
				})
			},
			insertImage() {
				let that = this;
				// #ifdef APP-PLUS || H5
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册或者相机选择
					success: (res) => {
						const tempFilePaths = res.tempFilePaths[0]
							uni.uploadFile({
								url: this.photoUrl + this.api,
								filePath: tempFilePaths,
								name: this.name,
								formData: {},
								success: (uploadFileRes) => {
									console.log('uploadFileRes',uploadFileRes);
									var obj = JSON.parse(uploadFileRes.data).data
									console.log('obj',obj);
									if (obj) {
										this.img = this.photoUrl + '/' + obj
										wx.showToast({
											title: obj.msg,
											icon: 'none'
										})
										this.editorCtx.insertImage({
											src: this.img,
											alt: '图像',
											success: function() {}
										})
									} else {
										wx.showToast({
											title: obj.msg,
											icon: 'none'
										})
									}
								}
							});
						
					},
					fail() {
						uni.showToast({
							title: '未授权访问相册权限，请授权后使用',
							icon: 'none'
						})
					}
				});
				// #endif
				// #ifdef MP
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album'],
					sizeType: 'compressed',
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFiles[0].tempFilePath;
						console.log(tempFilePaths)
						uni.uploadFile({
							url: this.photoUrl + this.api,
							filePath: tempFilePaths,
							name: this.name,
							formData: {},
							success: (uploadFileRes) => {
								console.log('uploadFileRes',uploadFileRes);
								var obj = JSON.parse(uploadFileRes.data).data
								console.log('obj',obj);
								if (obj) {
									this.img = this.photoUrl  + obj
									wx.showToast({
										title: obj.msg,
										icon: 'none'
									})
									this.editorCtx.insertImage({
										src: this.img,
										alt: '图像',
										success: function() {}
									})
								} else {
									wx.showToast({
										title: obj.msg,
										icon: 'none'
									})
								}
							}
						});
					}
				})
				// #endif
			}
		}
	}
</script>

<style>
	@import url('iconfont.css');

	.tabs {
		display: flex;
		justify-content: space-around;
		background-color: #FFFFFF;
	}

	.tabs .current {
		border-bottom: 2px solid #0369D6;
	}

	.tabs .tab {
		font-size: 32upx;
	}

	.main {
		padding: 20upx;
		background-color: #FFFFFF;
	}

	.main .item {
		display: flex;
		justify-content: space-between;
		line-height: 80upx;
		border-bottom: 1px solid #F2F2F2;
	}

	.main .item .left {
		min-width: 200upx;
	}

	.main .title {
		padding-bottom: 20upx;
		font-weight: bold;
		border-bottom: 1px solid #F2F2F2;
	}

	.main .textarea {
		border: 1px solid #F2F2F2;
	}

	.wrapper {
		padding: 0 10upx;
	}

	.iconfont {
		display: inline-block;
		width: 9%;
		cursor: pointer;
		font-size: 40upx !important;
		text-align: center;
		padding: 10upx 0;
	}

	.icon-rili4 {
		font-size: 48upx !important;
	}

	.icon-duanqianju,
	.icon-duanhouju,
	.icon-zitijiacu,
	.icon-zitixieti,
	.icon-zitixiahuaxian,
	.icon-shanchuxian {
		font-size: 36upx !important;
	}

	.toolbar {
		box-sizing: border-box;
		border-bottom: 0;
		margin-bottom: 10upx;
	}

	.ql-container {
		box-sizing: border-box;
		width: 100%;
		min-height: 300upx;
		height: auto;
		background: #fff;
		font-size: 32upx;
		line-height: 1;
		padding-bottom: 60upx;
	}

	/deep/ .ql-editor.ql-blank:before {
		font-size: 28upx;
		font-style: inherit;
	}

	.ql-active {
		color: #ff0000;
	}

	button {
		width: 150upx;
		font-size: 30upx;
	}

	.editor {
		color: #333;
		height: 186rpx;
		min-height: 186rpx;
		border-radius: 16rpx;
		border: 1px solid #D5D5D5;
		padding: 24rpx;
	}
</style>
